package com.my.controller;


import com.alibaba.fastjson.JSONObject;
import com.my.bean.MonthlyReport;
import com.my.bean.ResultPage;
import com.my.bean.User;
import com.my.bean.VoPage;
import com.my.service.MonthlyReportService;
import com.my.util.SessionUtil;
import com.my.util.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 周报 前端控制器
 * </p>
 *
 * @author 冯政尚
 * @since 2018-09-26
 */
@Controller
@RequestMapping("/monthlyReport")
public class MonthlyReportController {

    @Autowired
	MonthlyReportService monthlyReportService;
    //创建日志对象
    Logger logger = Logger.getLogger(DayController.class);


    /**
     * 添加月报
     * @param param
     * @return
     */
    @RequestMapping("/insertMonthly")
    @ResponseBody
    public boolean insertMonthlyReport(String param, HttpSession session){
		long areaid= ((User)session.getAttribute("user")).getAreaId();
        //把json字符串转换成实体类对象
        MonthlyReport monthlyReport = JSONObject.parseObject(param, MonthlyReport.class);
        //按照创建日期倒叙排序
        monthlyReport.setCreateTime(new Date().getTime());
        monthlyReport.setAreaid(areaid);
        boolean flg = monthlyReportService.insert(monthlyReport);
        String msg = flg?"添加成功"+monthlyReport:"添加失败";
        return  flg;
    }




    @RequestMapping("/selectMonthlyReport")
    @ResponseBody
    public List<MonthlyReport> selectMonthlyReport(String sendName, String logContent, String test6,HttpSession session){
        boolean admin=false;
        //如果事超级管理员  即可查看所有的房间  判断是否为超级管理员
        if(SessionUtil.getSessionUser(session).getRoles().getRoleValue().equals("9f77d5a8-75c6-4d06-adf9-a37e727d491a")){
            admin=true;
        }
        Map map=new HashMap();
        map.put("stra","");
        map.put("end","");
        map.put("sendName",sendName);
            if(!test6.equals("null")&&test6!=""&&test6!=null){
                //把空格换成空
                test6 = test6.replace(" ","");
                //街区字符串
                int i = StringUtils.jiequ(test6,"-",3);

                //开始时间
                String startDate = test6.substring(0,i);
                //结束时间
                String endDate =  test6.substring(i+1,test6.length());
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                Date dateStart=new Date();
                Date datEnd=new Date();
                try {
                    dateStart = format.parse(startDate);
                    datEnd = format.parse(endDate);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                //把开始时间日期转换成时间戳
                Long stra = dateStart.getTime();

                //把结束时间转换成时间戳
                Long end = datEnd.getTime();

                map.put("stra",stra);
                map.put("end",end);
            }
         map.put("logContent",logContent);
        Long areaid=((User)session.getAttribute("user")).getAreaId();
        List<MonthlyReport> monthlyReport= monthlyReportService.findMonthlyReport(map,areaid,admin);
        return monthlyReport;
    }









    /**
     * 查询月报
     * @param lin
     * @param data
     * @param voPage
     * @return
     */
    @RequestMapping("/selectMonthly")
    @ResponseBody
    public ResultPage<MonthlyReport> selectMonthly(String lin, String data, VoPage voPage,HttpSession session){
        boolean admin=false;
        //如果事超级管理员  即可查看所有的房间  判断是否为超级管理员
        if(SessionUtil.getSessionUser(session).getRoles().getRoleValue().equals("9f77d5a8-75c6-4d06-adf9-a37e727d491a")){
            admin=true;
        }
        Map map = new HashMap();
        map.put("lin",lin);
        map.put("data",data);
		Long areaid=((User)session.getAttribute("user")).getAreaId();
		ResultPage<MonthlyReport> resultPage = monthlyReportService.selectMyPage(map,voPage,areaid,admin);
        return resultPage;
    }


    /**
     * 修改日报
     * @param param
     * @return
     */
    @RequestMapping("/updateMonth")
    @ResponseBody
    public String updateDayy(String param){
        MonthlyReport day = JSONObject.parseObject(param, MonthlyReport.class);
        if(monthlyReportService.updateById(day)){
            return "修改成功";
        }else{
            return "修改失败";
        }

    }


    /**
     * 删除日报
     * @param id
     * @return
     */
    @RequestMapping("/deleteMonth")
    @ResponseBody
    public boolean deleteDay(Long id){
        return monthlyReportService.deleteById(id);
    }

    /**
     * 返回周报页面
     * @return
     */
    @RequestMapping("/monthlyReport")
    public String monthlyReport(){
        return "monthlyReport";
    }

    /**
     * 返回月报添加页面
     * @return
     */
    @RequestMapping("/monthlyReport_add")
    public String monthlyReportAdd(){
        return "/Alert/monthlyReport_add";
    }


    @RequestMapping("/monthlyReport_edit")
    public  String  monthlyReportEdit(){
        return "/Alert/monthlyReport_edit";
    }


    /**
     * 返回写周报
     */
    @RequestMapping("/seeMothe")
    public String seeMothe(){
        return "seeMoth";
    }



}
